

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>nlp_architect.models.crossling_emb &mdash; NLP Architect by Intel® AI Lab 0.5.2 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../_static/language_data.js"></script>
        <script type="text/javascript" src="../../../_static/install.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/nlp_arch_theme.css" type="text/css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Mono" type="text/css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:100,900" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html">
          

          
            
            <img src="../../../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../quick_start.html">Quick start</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../installation.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../publications.html">Publications</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../tutorials.html">Jupyter Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../model_zoo.html">Model Zoo</a></li>
</ul>
<p class="caption"><span class="caption-text">NLP/NLU Models</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../tagging/sequence_tagging.html">Sequence Tagging</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../sentiment.html">Sentiment Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../bist_parser.html">Dependency Parsing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../intent.html">Intent Extraction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../lm.html">Language Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../information_extraction.html">Information Extraction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../transformers.html">Transformers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../archived/additional.html">Additional Models</a></li>
</ul>
<p class="caption"><span class="caption-text">Optimized Models</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../quantized_bert.html">Quantized BERT</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../transformers_distillation.html">Transformers Distillation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../sparse_gnmt.html">Sparse Neural Machine Translation</a></li>
</ul>
<p class="caption"><span class="caption-text">Solutions</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../absa_solution.html">Aspect Based Sentiment Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../term_set_expansion.html">Set Expansion</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../trend_analysis.html">Trend Analysis</a></li>
</ul>
<p class="caption"><span class="caption-text">For Developers</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../generated_api/nlp_architect_api_index.html">nlp_architect API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../developer_guide.html">Developer Guide</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">NLP Architect by Intel® AI Lab</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
          <li><a href="../models.html">nlp_architect.models</a> &raquo;</li>
        
      <li>nlp_architect.models.crossling_emb</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for nlp_architect.models.crossling_emb</h1><div class="highlight"><pre>
<span></span><span class="c1"># ******************************************************************************</span>
<span class="c1"># Copyright 2017-2018 Intel Corporation</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1">#     http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1"># ******************************************************************************</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span><span class="p">,</span> <span class="n">division</span>

<span class="kn">import</span> <span class="nn">io</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">time</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">scipy</span>
<span class="kn">import</span> <span class="nn">tensorflow</span> <span class="k">as</span> <span class="nn">tf</span>


<div class="viewcode-block" id="Discriminator"><a class="viewcode-back" href="../../../generated_api/nlp_architect.models.html#nlp_architect.models.crossling_emb.Discriminator">[docs]</a><span class="k">class</span> <span class="nc">Discriminator</span><span class="p">:</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">input_data</span><span class="p">,</span> <span class="n">Y</span><span class="p">,</span> <span class="n">lr_ph</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">input_data</span> <span class="o">=</span> <span class="n">input_data</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lr_ph</span> <span class="o">=</span> <span class="n">lr_ph</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">do_ph</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">placeholder</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;dropout_ph&quot;</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">Y</span> <span class="o">=</span> <span class="n">Y</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">hid_dim</span> <span class="o">=</span> <span class="mi">2048</span>
        <span class="c1"># Build Graph</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_build_network_graph</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">disc_cost</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">disc_opt</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">map_opt</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">W</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">_build_network_graph</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Builds the basic inference graph for discriminator</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">with</span> <span class="n">tf</span><span class="o">.</span><span class="n">variable_scope</span><span class="p">(</span><span class="s2">&quot;Discriminator&quot;</span><span class="p">,</span> <span class="n">reuse</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">AUTO_REUSE</span><span class="p">):</span>
            <span class="n">w_init</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">contrib</span><span class="o">.</span><span class="n">layers</span><span class="o">.</span><span class="n">xavier_initializer</span><span class="p">()</span>
            <span class="n">noisy_input</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">dropout</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">input_data</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">do_ph</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;DO1&quot;</span><span class="p">)</span>
            <span class="n">fc1</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">layers</span><span class="o">.</span><span class="n">dense</span><span class="p">(</span>
                <span class="n">noisy_input</span><span class="p">,</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">hid_dim</span><span class="p">,</span>
                <span class="n">kernel_initializer</span><span class="o">=</span><span class="n">w_init</span><span class="p">,</span>
                <span class="n">activation</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">leaky_relu</span><span class="p">,</span>
                <span class="n">name</span><span class="o">=</span><span class="s2">&quot;Dense1&quot;</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="n">fc2</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">layers</span><span class="o">.</span><span class="n">dense</span><span class="p">(</span>
                <span class="n">fc1</span><span class="p">,</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">hid_dim</span><span class="p">,</span>
                <span class="n">kernel_initializer</span><span class="o">=</span><span class="n">w_init</span><span class="p">,</span>
                <span class="n">activation</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">leaky_relu</span><span class="p">,</span>
                <span class="n">name</span><span class="o">=</span><span class="s2">&quot;Dense2&quot;</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">prediction</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">layers</span><span class="o">.</span><span class="n">dense</span><span class="p">(</span><span class="n">fc2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">kernel_initializer</span><span class="o">=</span><span class="n">w_init</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;Dense_Sig&quot;</span><span class="p">)</span>

<div class="viewcode-block" id="Discriminator.build_train_graph"><a class="viewcode-back" href="../../../generated_api/nlp_architect.models.html#nlp_architect.models.crossling_emb.Discriminator.build_train_graph">[docs]</a>    <span class="k">def</span> <span class="nf">build_train_graph</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">disc_pred</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Builds training graph for discriminator</span>
<span class="sd">        Arguments:</span>
<span class="sd">             disc_pred(object): Discriminator instance</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Variables in discrimnator scope</span>
        <span class="n">disc_vars</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">get_collection</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">GraphKeys</span><span class="o">.</span><span class="n">TRAINABLE_VARIABLES</span><span class="p">,</span> <span class="s2">&quot;Discriminator&quot;</span><span class="p">)</span>
        <span class="c1"># Binary Cross entropy</span>
        <span class="n">disc_entropy</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">sigmoid_cross_entropy_with_logits</span><span class="p">(</span><span class="n">logits</span><span class="o">=</span><span class="n">disc_pred</span><span class="p">,</span> <span class="n">labels</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">Y</span><span class="p">)</span>
        <span class="c1"># Cost</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">disc_cost</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">reduce_mean</span><span class="p">(</span><span class="n">disc_entropy</span><span class="p">)</span>
        <span class="c1"># Optimizer</span>
        <span class="n">disc_opt</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">train</span><span class="o">.</span><span class="n">GradientDescentOptimizer</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lr_ph</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">disc_opt</span> <span class="o">=</span> <span class="n">disc_opt</span><span class="o">.</span><span class="n">minimize</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">disc_cost</span><span class="p">,</span> <span class="n">var_list</span><span class="o">=</span><span class="n">disc_vars</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="Generator"><a class="viewcode-back" href="../../../generated_api/nlp_architect.models.html#nlp_architect.models.crossling_emb.Generator">[docs]</a><span class="k">class</span> <span class="nc">Generator</span><span class="p">:</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">src_ten</span><span class="p">,</span> <span class="n">tgt_ten</span><span class="p">,</span> <span class="n">emb_dim</span><span class="p">,</span> <span class="n">batch_size</span><span class="p">,</span> <span class="n">smooth_val</span><span class="p">,</span> <span class="n">lr_ph</span><span class="p">,</span> <span class="n">beta</span><span class="p">,</span> <span class="n">vocab_size</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">src_ten</span> <span class="o">=</span> <span class="n">src_ten</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tgt_ten</span> <span class="o">=</span> <span class="n">tgt_ten</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">emb_dim</span> <span class="o">=</span> <span class="n">emb_dim</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span> <span class="o">=</span> <span class="n">batch_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">smooth_val</span> <span class="o">=</span> <span class="n">smooth_val</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">beta</span> <span class="o">=</span> <span class="n">beta</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lr_ph</span> <span class="o">=</span> <span class="n">lr_ph</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">vocab_size</span> <span class="o">=</span> <span class="n">vocab_size</span>

        <span class="c1"># Placeholders</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">src_ph</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">placeholder</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;src_ph&quot;</span><span class="p">,</span> <span class="n">shape</span><span class="o">=</span><span class="p">[</span><span class="kc">None</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tgt_ph</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">placeholder</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;tgt_ph&quot;</span><span class="p">,</span> <span class="n">shape</span><span class="o">=</span><span class="p">[</span><span class="kc">None</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>

        <span class="c1"># Build Graph</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_build_network_graph</span><span class="p">()</span>
        <span class="n">ortho_weight</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_build_ortho_graph</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">W</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assign_weight</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_assign_ortho_weight</span><span class="p">(</span><span class="n">ortho_weight</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">map_opt</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">W</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">_build_network_graph</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Builds basic inference graph for generator</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">with</span> <span class="n">tf</span><span class="o">.</span><span class="n">variable_scope</span><span class="p">(</span><span class="s2">&quot;Generator&quot;</span><span class="p">,</span> <span class="n">reuse</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">AUTO_REUSE</span><span class="p">):</span>
            <span class="c1"># Look up tables</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">src_emb</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">embedding_lookup</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">src_ten</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">src_ph</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;src_lut&quot;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">tgt_emb</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">embedding_lookup</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">tgt_ten</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">tgt_ph</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;tgt_lut&quot;</span><span class="p">)</span>
            <span class="c1"># Map them</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">mapWX</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_mapper</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">src_emb</span><span class="p">)</span>
            <span class="c1"># Concatenate them</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">X</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">mapWX</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">tgt_emb</span><span class="p">],</span> <span class="mi">0</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;X&quot;</span><span class="p">)</span>
            <span class="c1"># Set target for discriminator</span>
            <span class="n">Y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
            <span class="c1"># Label smoothing</span>
            <span class="n">Y</span><span class="p">[:</span> <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">smooth_val</span>
            <span class="n">Y</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span> <span class="p">:]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">smooth_val</span>
            <span class="c1"># Convert to tensor</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">Y</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">convert_to_tensor</span><span class="p">(</span><span class="n">Y</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;Y&quot;</span><span class="p">)</span>

<div class="viewcode-block" id="Generator.build_train_graph"><a class="viewcode-back" href="../../../generated_api/nlp_architect.models.html#nlp_architect.models.crossling_emb.Generator.build_train_graph">[docs]</a>    <span class="k">def</span> <span class="nf">build_train_graph</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">disc_pred</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Builds training graph for generator</span>
<span class="sd">        Arguments:</span>
<span class="sd">            disc_pred(object): Discriminator instance</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Variables in Mapper scope</span>
        <span class="n">map_vars</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">get_collection</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">GraphKeys</span><span class="o">.</span><span class="n">TRAINABLE_VARIABLES</span><span class="p">,</span> <span class="s2">&quot;Generator/Mapper&quot;</span><span class="p">)</span>
        <span class="c1"># Binary Cross entropy</span>
        <span class="n">map_entropy</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">sigmoid_cross_entropy_with_logits</span><span class="p">(</span><span class="n">logits</span><span class="o">=</span><span class="n">disc_pred</span><span class="p">,</span> <span class="n">labels</span><span class="o">=</span><span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">Y</span><span class="p">))</span>
        <span class="c1"># Cost</span>
        <span class="n">map_cost</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">reduce_mean</span><span class="p">(</span><span class="n">map_entropy</span><span class="p">)</span>
        <span class="n">map_opt</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">train</span><span class="o">.</span><span class="n">GradientDescentOptimizer</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lr_ph</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">map_opt</span> <span class="o">=</span> <span class="n">map_opt</span><span class="o">.</span><span class="n">minimize</span><span class="p">(</span><span class="n">map_cost</span><span class="p">,</span> <span class="n">var_list</span><span class="o">=</span><span class="n">map_vars</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_build_ortho_graph</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">W</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Builds a graph to orthogonalize weight W</span>
<span class="sd">        Arguments:</span>
<span class="sd">            W (Tensor): Weight in the mapper</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">with</span> <span class="n">tf</span><span class="o">.</span><span class="n">variable_scope</span><span class="p">(</span><span class="s2">&quot;Ortho&quot;</span><span class="p">,</span> <span class="n">reuse</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">AUTO_REUSE</span><span class="p">):</span>
            <span class="n">a</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">scalar_mul</span><span class="p">((</span><span class="mi">1</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">beta</span><span class="p">),</span> <span class="n">W</span><span class="p">)</span>  <span class="c1"># (1+B)W</span>
            <span class="n">b</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="n">W</span><span class="p">),</span> <span class="n">W</span><span class="p">)</span>  <span class="c1"># WWt</span>
            <span class="n">c</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">W</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>  <span class="c1"># W(W.Wt)</span>
            <span class="n">d</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">scalar_mul</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">beta</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span>  <span class="c1"># B(W.Wt)W</span>
            <span class="n">ortho_weight</span> <span class="o">=</span> <span class="n">a</span> <span class="o">-</span> <span class="n">d</span>
            <span class="k">return</span> <span class="n">ortho_weight</span>

    <span class="k">def</span> <span class="nf">_assign_ortho_weight</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ortho_weight</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Builds a graph to assign weight W after it is orthogonalized</span>
<span class="sd">        Arguments:</span>
<span class="sd">             ortho_weight(Tensor): Weight after it is orthogonalized</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">tf</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">W</span><span class="p">,</span> <span class="n">ortho_weight</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_mapper</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">src_emb</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Learns WX mapping to make ||WX-Y|| smaller</span>
<span class="sd">        Arguments:</span>
<span class="sd">             src_emb(Tensor): Source embeddings after lookup</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">with</span> <span class="n">tf</span><span class="o">.</span><span class="n">variable_scope</span><span class="p">(</span><span class="s2">&quot;Mapper&quot;</span><span class="p">,</span> <span class="n">reuse</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">AUTO_REUSE</span><span class="p">):</span>
            <span class="c1"># Initialize as an eye of emb_dim x emb_dim</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">W</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;W&quot;</span><span class="p">,</span> <span class="n">initial_value</span><span class="o">=</span><span class="n">tf</span><span class="o">.</span><span class="n">eye</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">emb_dim</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">emb_dim</span><span class="p">))</span>
            <span class="c1"># Do Matrix Multiply</span>
            <span class="n">WX</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">src_emb</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">W</span><span class="p">)</span>
            <span class="c1"># Returns map and weight handles</span>
            <span class="k">return</span> <span class="n">WX</span></div>


<div class="viewcode-block" id="WordTranslator"><a class="viewcode-back" href="../../../generated_api/nlp_architect.models.html#nlp_architect.models.crossling_emb.WordTranslator">[docs]</a><span class="k">class</span> <span class="nc">WordTranslator</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Main network which does cross-lingual embeddings training</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">hparams</span><span class="p">,</span> <span class="n">src_vec</span><span class="p">,</span> <span class="n">tgt_vec</span><span class="p">,</span> <span class="n">vocab_size</span><span class="p">):</span>
        <span class="c1"># Hyperparameters</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span> <span class="o">=</span> <span class="n">hparams</span><span class="o">.</span><span class="n">batch_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">smooth_val</span> <span class="o">=</span> <span class="n">hparams</span><span class="o">.</span><span class="n">smooth_val</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">beta</span> <span class="o">=</span> <span class="n">hparams</span><span class="o">.</span><span class="n">beta</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">most_freq</span> <span class="o">=</span> <span class="n">hparams</span><span class="o">.</span><span class="n">most_freq</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">emb_dim</span> <span class="o">=</span> <span class="n">hparams</span><span class="o">.</span><span class="n">emb_dim</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">vocab_size</span> <span class="o">=</span> <span class="n">vocab_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">disc_runs</span> <span class="o">=</span> <span class="n">hparams</span><span class="o">.</span><span class="n">disc_runs</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">iters_epoch</span> <span class="o">=</span> <span class="n">hparams</span><span class="o">.</span><span class="n">iters_epoch</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">src_vec</span> <span class="o">=</span> <span class="n">src_vec</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tgt_vec</span> <span class="o">=</span> <span class="n">tgt_vec</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">src_ten</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">convert_to_tensor</span><span class="p">(</span><span class="n">src_vec</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tgt_ten</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">convert_to_tensor</span><span class="p">(</span><span class="n">tgt_vec</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">save_dir</span> <span class="o">=</span> <span class="n">hparams</span><span class="o">.</span><span class="n">weight_dir</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">slang</span> <span class="o">=</span> <span class="n">hparams</span><span class="o">.</span><span class="n">src_lang</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tlang</span> <span class="o">=</span> <span class="n">hparams</span><span class="o">.</span><span class="n">tgt_lang</span>

        <span class="c1"># Placeholders</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lr_ph</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">placeholder</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">float32</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;lrPh&quot;</span><span class="p">)</span>
        <span class="c1"># Build Graph</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_build_network_graph</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_build_train_graph</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_build_network_graph</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Builds inference graph for the GAN</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">generator</span> <span class="o">=</span> <span class="n">Generator</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">src_ten</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">tgt_ten</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">emb_dim</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">smooth_val</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lr_ph</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">beta</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">vocab_size</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span> <span class="o">=</span> <span class="n">Discriminator</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">X</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">Y</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">lr_ph</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_build_train_graph</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Builds training graph for the GAN</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">build_train_graph</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="o">.</span><span class="n">prediction</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="o">.</span><span class="n">build_train_graph</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="o">.</span><span class="n">prediction</span><span class="p">)</span>

<div class="viewcode-block" id="WordTranslator.report_metrics"><a class="viewcode-back" href="../../../generated_api/nlp_architect.models.html#nlp_architect.models.crossling_emb.WordTranslator.report_metrics">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">report_metrics</span><span class="p">(</span><span class="n">iters</span><span class="p">,</span> <span class="n">n_words_proc</span><span class="p">,</span> <span class="n">disc_cost_acc</span><span class="p">,</span> <span class="n">tic</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Reports metrics of how training is going</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">iters</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">iters</span> <span class="o">%</span> <span class="mi">500</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">mean_cost</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="nb">sum</span><span class="p">(</span><span class="n">disc_cost_acc</span><span class="p">)</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">disc_cost_acc</span><span class="p">))</span>
            <span class="nb">print</span><span class="p">(</span>
                <span class="nb">str</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">n_words_proc</span> <span class="o">/</span> <span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">-</span> <span class="n">tic</span><span class="p">)))</span>
                <span class="o">+</span> <span class="s2">&quot; Samples/Sec - Iter &quot;</span>
                <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">iters</span><span class="p">)</span>
                <span class="o">+</span> <span class="s2">&quot; Discriminator Cost: &quot;</span>
                <span class="o">+</span> <span class="n">mean_cost</span>
            <span class="p">)</span>
            <span class="c1"># Reset instrumentation</span>
            <span class="k">del</span> <span class="n">disc_cost_acc</span>
            <span class="n">disc_cost_acc</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">n_words_proc</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="n">tic</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span></div>

<div class="viewcode-block" id="WordTranslator.run_generator"><a class="viewcode-back" href="../../../generated_api/nlp_architect.models.html#nlp_architect.models.crossling_emb.WordTranslator.run_generator">[docs]</a>    <span class="k">def</span> <span class="nf">run_generator</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sess</span><span class="p">,</span> <span class="n">local_lr</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Runs generator part of GAN</span>
<span class="sd">        Arguments:</span>
<span class="sd">            sess(tf.session): Tensorflow Session</span>
<span class="sd">            local_lr(float): Learning rate</span>
<span class="sd">        Returns:</span>
<span class="sd">            Returns number of words processed</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Generate random ids to look up</span>
        <span class="n">src_ids</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">vocab_size</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">,</span> <span class="n">replace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="n">tgt_ids</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">vocab_size</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">,</span> <span class="n">replace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="n">train_dict</span> <span class="o">=</span> <span class="p">{</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">src_ph</span><span class="p">:</span> <span class="n">src_ids</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">tgt_ph</span><span class="p">:</span> <span class="n">tgt_ids</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="o">.</span><span class="n">do_ph</span><span class="p">:</span> <span class="mf">1.0</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lr_ph</span><span class="p">:</span> <span class="n">local_lr</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">map_opt</span><span class="p">,</span> <span class="n">feed_dict</span><span class="o">=</span><span class="n">train_dict</span><span class="p">)</span>
        <span class="c1"># Run orthogonalize</span>
        <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">assign_weight</span><span class="p">)</span>
        <span class="k">return</span> <span class="mi">2</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span></div>

<div class="viewcode-block" id="WordTranslator.run_discriminator"><a class="viewcode-back" href="../../../generated_api/nlp_architect.models.html#nlp_architect.models.crossling_emb.WordTranslator.run_discriminator">[docs]</a>    <span class="k">def</span> <span class="nf">run_discriminator</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sess</span><span class="p">,</span> <span class="n">local_lr</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Runs discriminator part of GAN</span>
<span class="sd">        Arguments:</span>
<span class="sd">            sess(tf.session): Tensorflow Session</span>
<span class="sd">            local_lr(float): Learning rate</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Generate random ids to look up</span>
        <span class="n">src_ids</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">most_freq</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">,</span> <span class="n">replace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="n">tgt_ids</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">most_freq</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">,</span> <span class="n">replace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="n">train_dict</span> <span class="o">=</span> <span class="p">{</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">src_ph</span><span class="p">:</span> <span class="n">src_ids</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">tgt_ph</span><span class="p">:</span> <span class="n">tgt_ids</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="o">.</span><span class="n">do_ph</span><span class="p">:</span> <span class="mf">0.9</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lr_ph</span><span class="p">:</span> <span class="n">local_lr</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span>
            <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="o">.</span><span class="n">disc_cost</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="o">.</span><span class="n">disc_opt</span><span class="p">],</span> <span class="n">feed_dict</span><span class="o">=</span><span class="n">train_dict</span>
        <span class="p">)</span></div>

<div class="viewcode-block" id="WordTranslator.run"><a class="viewcode-back" href="../../../generated_api/nlp_architect.models.html#nlp_architect.models.crossling_emb.WordTranslator.run">[docs]</a>    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sess</span><span class="p">,</span> <span class="n">local_lr</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Runs whole GAN</span>
<span class="sd">        Arguments:</span>
<span class="sd">            sess(tf.session): Tensorflow Session</span>
<span class="sd">            local_lr(float): Learning rate</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">disc_cost_acc</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">n_words_proc</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">tic</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">iters</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">iters_epoch</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">):</span>
            <span class="c1"># 1.Run the discriminator</span>
            <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">disc_runs</span><span class="p">):</span>
                <span class="n">disc_result</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">run_discriminator</span><span class="p">(</span><span class="n">sess</span><span class="p">,</span> <span class="n">local_lr</span><span class="p">)</span>
                <span class="n">disc_cost_acc</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">disc_result</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
            <span class="c1"># 2.Run the Generator</span>
            <span class="n">n_words_proc</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">run_generator</span><span class="p">(</span><span class="n">sess</span><span class="p">,</span> <span class="n">local_lr</span><span class="p">)</span>
            <span class="c1"># 3.Report the metrics</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">report_metrics</span><span class="p">(</span><span class="n">iters</span><span class="p">,</span> <span class="n">n_words_proc</span><span class="p">,</span> <span class="n">disc_cost_acc</span><span class="p">,</span> <span class="n">tic</span><span class="p">)</span></div>

<div class="viewcode-block" id="WordTranslator.set_lr"><a class="viewcode-back" href="../../../generated_api/nlp_architect.models.html#nlp_architect.models.crossling_emb.WordTranslator.set_lr">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">set_lr</span><span class="p">(</span><span class="n">local_lr</span><span class="p">,</span> <span class="n">drop_lr</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Drops learning rate based on CSLS criterion</span>
<span class="sd">        Arguments:</span>
<span class="sd">            local_lr(float): Learning Rate</span>
<span class="sd">            drop_lr(bool): Drop learning rate by 2 if True</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">new_lr</span> <span class="o">=</span> <span class="n">local_lr</span> <span class="o">*</span> <span class="mf">0.98</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Dropping learning rate to &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">new_lr</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot; from &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">local_lr</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">drop_lr</span><span class="p">:</span>
            <span class="n">new_lr</span> <span class="o">=</span> <span class="n">new_lr</span> <span class="o">/</span> <span class="mf">2.0</span>
            <span class="nb">print</span><span class="p">(</span>
                <span class="s2">&quot;Dividing learning rate by 2 as validation criterion</span><span class="se">\</span>
<span class="s2">                   decreased. New lr is &quot;</span>
                <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">new_lr</span><span class="p">)</span>
            <span class="p">)</span>
        <span class="k">return</span> <span class="n">new_lr</span></div>

<div class="viewcode-block" id="WordTranslator.save_model"><a class="viewcode-back" href="../../../generated_api/nlp_architect.models.html#nlp_architect.models.crossling_emb.WordTranslator.save_model">[docs]</a>    <span class="k">def</span> <span class="nf">save_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">save_model</span><span class="p">,</span> <span class="n">sess</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Saves W in mapper as numpy array based on CSLS criterion</span>
<span class="sd">        Arguments:</span>
<span class="sd">            save_model(bool): Save model if True</span>
<span class="sd">            sess(tf.session): Tensorflow Session</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">save_model</span><span class="p">:</span>
            <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Saving model ....&quot;</span><span class="p">)</span>
            <span class="n">model_W</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">W</span><span class="p">)</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">save_dir</span><span class="p">,</span> <span class="s2">&quot;W_best_mapping&quot;</span><span class="p">)</span>
            <span class="n">np</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">model_W</span><span class="p">)</span></div>

<div class="viewcode-block" id="WordTranslator.apply_procrustes"><a class="viewcode-back" href="../../../generated_api/nlp_architect.models.html#nlp_architect.models.crossling_emb.WordTranslator.apply_procrustes">[docs]</a>    <span class="k">def</span> <span class="nf">apply_procrustes</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sess</span><span class="p">,</span> <span class="n">final_pairs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Applies procrustes to W matrix for better mapping</span>
<span class="sd">        Arguments:</span>
<span class="sd">            sess(tf.session): Tensorflow Session</span>
<span class="sd">            final_pairs(ndarray): Array of pairs which are mutual neighbors</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Applying solution of Procrustes problem to get better mapping...&quot;</span><span class="p">)</span>
        <span class="n">proc_dict</span> <span class="o">=</span> <span class="p">{</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">src_ph</span><span class="p">:</span> <span class="n">final_pairs</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">],</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">tgt_ph</span><span class="p">:</span> <span class="n">final_pairs</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">],</span>
        <span class="p">}</span>
        <span class="n">A</span><span class="p">,</span> <span class="n">B</span> <span class="o">=</span> <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">src_emb</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">tgt_emb</span><span class="p">],</span> <span class="n">feed_dict</span><span class="o">=</span><span class="n">proc_dict</span><span class="p">)</span>
        <span class="c1"># pylint: disable=no-member</span>
        <span class="n">R</span> <span class="o">=</span> <span class="n">scipy</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">orthogonal_procrustes</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">)</span>
        <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">W</span><span class="p">,</span> <span class="n">R</span><span class="p">[</span><span class="mi">0</span><span class="p">]))</span></div>

<div class="viewcode-block" id="WordTranslator.generate_xling_embed"><a class="viewcode-back" href="../../../generated_api/nlp_architect.models.html#nlp_architect.models.crossling_emb.WordTranslator.generate_xling_embed">[docs]</a>    <span class="k">def</span> <span class="nf">generate_xling_embed</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sess</span><span class="p">,</span> <span class="n">src_dict</span><span class="p">,</span> <span class="n">tgt_dict</span><span class="p">,</span> <span class="n">tgt_vec</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates cross lingual embeddings</span>
<span class="sd">        Arguments:</span>
<span class="sd">             sess(tf.session): Tensorflow session</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Generating Cross-lingual embeddings...&quot;</span><span class="p">)</span>
        <span class="n">src_emb_x</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">batch_size</span> <span class="o">=</span> <span class="mi">512</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">vocab_size</span><span class="p">,</span> <span class="n">batch_size</span><span class="p">):</span>
            <span class="n">sids</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="nb">min</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="n">batch_size</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">vocab_size</span><span class="p">))]</span>
            <span class="n">src_emb_x</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="n">sess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">mapWX</span><span class="p">,</span> <span class="n">feed_dict</span><span class="o">=</span><span class="p">{</span><span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">src_ph</span><span class="p">:</span> <span class="n">sids</span><span class="p">})</span>
            <span class="p">)</span>
        <span class="n">src_emb_x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">(</span><span class="n">src_emb_x</span><span class="p">)</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Writing cross-lingual embeddings to file...&quot;</span><span class="p">)</span>
        <span class="n">src_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">save_dir</span><span class="p">,</span> <span class="s2">&quot;vectors-</span><span class="si">%s</span><span class="s2">.txt&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">slang</span><span class="p">)</span>
        <span class="n">tgt_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">save_dir</span><span class="p">,</span> <span class="s2">&quot;vectors-</span><span class="si">%s</span><span class="s2">.txt&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">tlang</span><span class="p">)</span>
        <span class="k">with</span> <span class="n">io</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">src_path</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s2">&quot;utf-8&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%i</span><span class="s2"> </span><span class="si">%i</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">src_emb_x</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">src_dict</span><span class="p">)):</span>
                <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> </span><span class="si">%s</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">src_dict</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%.5f</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">src_emb_x</span><span class="p">[</span><span class="n">i</span><span class="p">])))</span>

        <span class="k">with</span> <span class="n">io</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">tgt_path</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s2">&quot;utf-8&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%i</span><span class="s2"> </span><span class="si">%i</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">tgt_vec</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">tgt_dict</span><span class="p">)):</span>
                <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> </span><span class="si">%s</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">tgt_dict</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%.5f</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">tgt_vec</span><span class="p">[</span><span class="n">i</span><span class="p">])))</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>